const db = require("../models");
const DAO = require("../dao/DAO");
const logger = require("../utils/utils.logger");
const model = db.info;

// 创建用户
exports.create = async (req, res, next) => {
	try {
		const pm = req.body;
		if (!pm.registName) return res.sendResultAto(null, 605, '录入人不能为空')
		if (!pm.infoName) return res.sendResultAto(null, 605, '被录入人不能为空')

		const params = { registName: pm.registName, infoName: pm.infoName, infoImgUrl: `uploads/${req.file.filename}` }
		// 新增
		await model.create(params)
		res.sendResultAto(null, 200, '添加成功！')
	} catch (error) {
		logger.error(error)
		next('服务器内部错误')
	}
};

// 查询用户
exports.findAll = async (req, res, next) => {
	try {
		const params = {
			params:{

			},
			...req.query,
			sort: {
				prop: 'infoId',
				order: 'desc',
			}
		}
		// if (req.auth.villageId) {
		// 	params.params.villageId = req.auth.villageId
		// }

		const list = await DAO.list(model, params)
		res.sendResult(list)
	} catch (error) {
		logger.error(error)
		next('服务器内部错误')
	}
};

// 删除用户
exports.delete = async (req, res, next) => {
	try {
		const infoId = req.params.id;
		// 请求验证
		if (!infoId) return res.sendResult({ data: '', code: 605, message: "ID不能为空！" })
		const data = await model.destroy({ where: { infoId } })
		if (data) {
			res.sendResultAto(null, 200, '删除成功！')
		} else {
			res.sendResultAto(null, 500, '数据不存在')
		}
	} catch (error) {
		logger.error(error)
		next('服务器内部错误')
	}
};




